/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.nii.qlks.BusinessLogicLayer;

import edu.nii.qlks.DataAccessLayer.MSSQLConnectUnit;
import edu.nii.qlks.DataTransferObject.TaiKhoanInfo;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Vector;

/**
 *
 * @author MARS_NHO
 */
public class TaiKhoanBLL {
    MSSQLConnectUnit _connect;
    ResultSet  _result;
 
    
    public Vector LayDS(String Condition, String OderBy) throws Exception{
        Vector vt = new Vector();
        
        _connect = edu.nii.qlks.BusinessLogicLayer.BusinessLogicAbstract.getConnection();
        _result = _connect.Select("TAIKHOAN", Condition, OderBy);
        
        while (_result.next()){
            Vector vt_row = new Vector();
            vt_row.add(_result.getString(1));
            vt_row.add(_result.getString(2));
            vt_row.add(_result.getString(3));
            vt_row.add(_result.getString(4));
            vt.add(vt_row);
        }
        _connect.Close();
        return vt;
    }
    
    public Vector LayDS(String Condition) throws Exception{
         return LayDS(Condition, null);
    }
    
    public Vector LayDS() throws Exception{
        return LayDS(null);
    }
    
    public void Insert(TaiKhoanInfo taiKhoanInfo) throws Exception{
        _connect = edu.nii.qlks.BusinessLogicLayer.BusinessLogicAbstract.getConnection();
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("MaTaiKhoan", taiKhoanInfo.getMaTaiKhoan());
        map.put("TenTaiKhoan", taiKhoanInfo.getTenTaiKhoan());
        map.put("MaLoaiTaiKhoan", taiKhoanInfo.getMaLoaiTaiKhoan());
        map.put("MatKhau", taiKhoanInfo.getMatKhau());
        _connect.Insert("TAIKHOAN", map);
    }
    
     
    public void Inserts(HashSet<TaiKhoanInfo> taiKhoanInfos) throws Exception{
        for (TaiKhoanInfo taiKhoanInfo:taiKhoanInfos) {
            this.Insert(taiKhoanInfo);
        }
    }
    
    public void Update(TaiKhoanInfo taiKhoanInfo) throws Exception{
        _connect = edu.nii.qlks.BusinessLogicLayer.BusinessLogicAbstract.getConnection();
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("TenTaiKhoan", taiKhoanInfo.getTenTaiKhoan());
        map.put("MaLoaiTaiKhoan", taiKhoanInfo.getMaLoaiTaiKhoan());
        map.put("MatKhau", taiKhoanInfo.getMatKhau());
        _connect.Update("TAIKHOAN", map, "MaTaiKhoan = '" + taiKhoanInfo.getMaTaiKhoan() + "'");
    }
    
    public void Updates(HashSet<TaiKhoanInfo> taiKhoanInfos) throws Exception{
        for (TaiKhoanInfo taiKhoanInfo:taiKhoanInfos){
            this.Update(taiKhoanInfo);
        }
    }
    
    public void Delete(TaiKhoanInfo taiKhoanInfo) throws Exception{
        _connect = edu.nii.qlks.BusinessLogicLayer.BusinessLogicAbstract.getConnection();
        _connect.Delete("TAIKHOAN", "MaTaiKhoan = '" + taiKhoanInfo.getMaTaiKhoan() + "'");
    }
    
    public void Deletes(HashSet<TaiKhoanInfo> taiKhoanInfos) throws Exception{
         for (TaiKhoanInfo taiKhoanInfo:taiKhoanInfos){
            this.Delete(taiKhoanInfo);
        }
    }
    
    public String TaoMaTuDong(String maGoc, int batDau){
        String ma = edu.nii.qlks.BusinessLogicLayer.BusinessLogicAbstract.TaoMaTuDong(maGoc, batDau);
        return ma;
    }
    
    public Vector LayTT(String TenTaiKhoan, String MatKhau) throws Exception{
        Vector vt = new Vector();
        
        _connect = edu.nii.qlks.BusinessLogicLayer.BusinessLogicAbstract.getConnection();
        _result = _connect.Select("TAIKHOAN", 
                "TenTaiKhoan = '" + TenTaiKhoan + "' " +
                "and MatKhau = '" + MatKhau +"' ", 
                null);
        
        while (_result.next()){
            Vector vt_row = new Vector();
            vt_row.add(_result.getString(1));
            vt_row.add(_result.getString(2));
            vt_row.add(_result.getString(3));
            vt_row.add(_result.getString(4));
            vt.add(vt_row);
        }
        _connect.Close();
        return vt;
    }
    
    public Vector Lay_MaTaiKhoan(String TenTaiKhoan) throws Exception{
        Vector vt = new Vector();
        
        _connect = edu.nii.qlks.BusinessLogicLayer.BusinessLogicAbstract.getConnection();
        _result = _connect.Select("TAIKHOAN.MaTaiKhoan", "TAIKHOAN", 
                "TAIKHOAN.TenTaiKhoan = '" + TenTaiKhoan + "' " , 
                null);
        
        while (_result.next()){
            Vector vt_row = new Vector();
            vt_row.add(_result.getString(1));
            vt.add(vt_row);
        }
        _connect.Close();
        return vt;
    }
    
    public Vector Lay_MaLoaiTaiKhoan(String TenTaiKhoan) throws Exception{
        Vector vt = new Vector();
        
        _connect = edu.nii.qlks.BusinessLogicLayer.BusinessLogicAbstract.getConnection();
        _result = _connect.Select("LOAITAIKHOAN.MaLoaiTaiKhoan, LOAITAIKHOAN.TenLoaiTaiKhoan", "TAIKHOAN, LOAITAIKHOAN", 
                "LOAITAIKHOAN.MaLoaiTaiKhoan = TAIKHOAN.MaLoaiTaiKhoan and TAIKHOAN.TenTaiKhoan = '" + TenTaiKhoan + "' " , 
                null);
        
        while (_result.next()){
            Vector vt_row = new Vector();
            vt_row.add(_result.getString(1));
            vt_row.add(_result.getString(2));
            vt.add(vt_row);
        }
        _connect.Close();
        return vt;
    }
    
    public Vector LayDS_TenLoaiTaiKhoan() throws Exception{
        Vector vt = new Vector();
        
        _connect = edu.nii.qlks.BusinessLogicLayer.BusinessLogicAbstract.getConnection();
        _result = _connect.Select("TAIKHOAN.MaTaiKhoan, TAIKHOAN.TenTaiKhoan, " + 
                "LOAITAIKHOAN.MaLoaiTaiKhoan, LOAITAIKHOAN.TenLoaiTaiKhoan , " +
                "TAIKHOAN.MatKhau", "TAIKHOAN, LOAITAIKHOAN", 
                "LOAITAIKHOAN.MaLoaiTaiKhoan = TAIKHOAN.MaLoaiTaiKhoan ", 
                null);
        
        while (_result.next()){
            Vector vt_row = new Vector();
            vt_row.add(_result.getString(1));
            vt_row.add(_result.getString(2));
            vt_row.add(_result.getString(3));
            vt_row.add(_result.getString(4));
            vt_row.add(_result.getString(5));
            vt.add(vt_row);
        }
        _connect.Close();
        return vt;
    }
}
